//
//  NMyCollectViewController.m
//  News
//
//  Created by zhangtao on 2018/10/15.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NMyCollectViewController.h"
#import "MyFavoritesModel.h"

@interface NMyCollectViewController ()

#define oneImgIdentifier @"oneImgIdentifier"
#define threeImgIdentifier @"threeImgIdentifier"
#define noImgIdentifier @"noImgIdentifier"
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation NMyCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainOneImgCell" bundle:nil] forCellReuseIdentifier:oneImgIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainThreeImgCell" bundle:nil] forCellReuseIdentifier:threeImgIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"NMainNoImgCell" bundle:nil] forCellReuseIdentifier:noImgIdentifier];

    [self addRefreshFunction];
    [self addLoadMoreFunction];
}

#pragma mark AddTableViewPlugin
- (void)addRefreshFunction
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader
                                     headerWithRefreshingTarget:self
                                     refreshingAction:@selector(refreshAction)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    self.tableView.mj_header = header;
}

- (void)addLoadMoreFunction
{
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter
                                        footerWithRefreshingTarget:self
                                        refreshingAction:@selector(loadMoreAction)];
    // 设置footer
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.automaticallyHidden = YES;
}

#pragma mark RefreshAction
- (void)refreshAction
{
    self.pageIndex = 1;
    [self getData:1];
}

- (void)loadMoreAction
{
    self.pageIndex ++;
    [self getData:self.pageIndex];
}

- (void)getData:(NSInteger)pageIndex
{
    [[NHTTPManage shareInstance]afGet:HTTP_GET_GetMyFavorites andParam:@{@"token":TOKEN,@"cuid":CUID,@"cp":@(pageIndex)} success:^(id  _Nonnull responseObject) {
        //结束头部刷新
        [self.tableView.mj_header endRefreshing];
        //结束尾部刷新
        [self.tableView.mj_footer endRefreshing];
        if (self.dataArray.count > 0 && self.pageIndex == 1) {
            [self.dataArray removeAllObjects];
        }
        [responseObject[@"Data"] enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MyFavoritesModel *model = [MyFavoritesModel modelObjectWithDictionary:obj];
            [self.dataArray addObject:model];
        }];
        if ([responseObject[@"Data"] count] == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    } failture:^(id  _Nonnull error) {
        //结束头部刷新
        [self.tableView.mj_header endRefreshing];
        //结束尾部刷新
        [self.tableView.mj_footer endRefreshing];
        NSLog(@"%@",error);
    }];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyFavoritesModel *model = self.dataArray[indexPath.row];
    if (model.articleType == 1) {
        //380:110
        return ACEWidth*11/38;
    } else if (model.articleType == 4) {
        //390:180
        return ACEWidth*18/39;
    } else if (model.articleType == 5) {
        //380:120
        return ACEWidth*5/19;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyFavoritesModel *model = self.dataArray[indexPath.row];
    if (model.articleType == 1) {
        NMainOneImgCell *cell = [tableView dequeueReusableCellWithIdentifier:oneImgIdentifier];
        [cell bindData:model];
        return cell;
    } else if (model.articleType == 4) {
        NMainThreeImgCell *cell = [tableView dequeueReusableCellWithIdentifier:threeImgIdentifier];
        [cell bindData:model];
        return cell;
    } else if (model.articleType == 5) {
        NMainNoImgCell *cell = [tableView dequeueReusableCellWithIdentifier:noImgIdentifier];
        [cell bindData:model];
        return cell;
    }
    return nil;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
   
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSLog(@"删除。。。");
    }
}

// 修改编辑按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
